home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14407 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: news.mcs.net!usenet
  2. From: Mike Young <mikey@mcs.com>
  3. Newsgroups: comp.lang.ada,comp.lang.c++
  4. Subject: Re: some questions re. Ada/GNAT from a C++/GCC user
  5. Date: Sat, 30 Mar 1996 01:04:25 -0600
  6. Organization: Fen Software, Inc.
  7. Message-ID: <315CDCF9.31AF@mcs.com>
  8. References: <wnewmanDoxrCp.DKv@netcom.com> <4je9ju$174r@watnews1.watson.ibm.com> <ROGOFF.96Mar28134118@sccm.Stanford.EDU> <4jhe1v$m0g@dayuc.dayton.saic.com>
  9. NNTP-Posting-Host: mikey.pr.mcs.net
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0GoldB1 (Win95; I)
  14.  
  15. John G. Volan wrote:
  16. > In article <315AC5E7.3A77@escmail.orl.mmc.com>
  17. > Ted Dennison, dennison@escmail.orl.mmc.com writes:
  18. > >If you want to declare variables later in the code just
  19. > >use a declare block:
  20. > >   declare
  21. > >      Var1 : My_Type;
  22. > >      ...
  23. > >   begin
  24. > >      Var1 := My_Value;
  25. > >      ...
  26. > >   end;
  27. > >
  28. > >This has the added benefit of limiting the scope of Var1 to the
  29. > >begin..end  block of code.
  30. > There is another benefit, but it isn't obvious unless you take into
  31. > account the concurrency aspects of Ada:
  32. > A C programmer would complain about having to introduce an extra
  33. > begin/end block in order to introduce variables.  Isn't the scope
  34. > of a variable well-defined in C, even without this extra baggage?
  35. > It just extends from the declaration to the end of whatever block
  36. > you're already in.
  37. > An Ada programmer would counter that, in Ada, some variables might be
  38. > instances of _task_ types.  By definition, a task object gets created
  39. > when its declaration is elaborated, but it does not get _activated_
  40. > (i.e., it doesn't start executing its statements) until you hit the
  41. > "begin" of the enclosing block.  This guarantees that everything in 
  42.  
  43.   [ ... some worthy thoughts snipped ... ]
  44.  
  45. > C, of course, has no concurrency abstractions built in (nor does C++,
  46. > last time I checked), so, gee, I guess this isn't an issue in C. :-)
  47.  
  48. ============
  49. Hmmmm. Lacking a language based synch method doesn't mean we always go 
  50. straight to the semaphore library. I commonly use thin wrappers to guard 
  51. precious resources -- for example: mutexes, semaphores, files, 
  52. or database sessions. Local scoping allows precise control of lifetimes, 
  53. and ensure release even when exceptions are possible:
  54.  
  55. void foo ()
  56. {
  57.    //-- some setup code here, perhaps
  58.    ...
  59.    {               // local scope
  60.      LocalMutex<BazType> foo(mutexHandle);
  61.      BazType & bar = foo.openResource();
  62.      ...           // use the protected resource
  63.    }               // end local scope. LocalMutex releases the resource
  64.    ...
  65. }
  66.